'''
东方财富人气榜数据爬取
网址：https://guba.eastmoney.com/rank/

东方财富： https://www.eastmoney.com/



'''

import re
import requests

cookies = {
    'websitepoptg_api_time': '1727759287081',
    'st_si': '57823914314076',
    'qgqp_b_id': '5b9d12d1397dfb64500d7fa60eb92f4c',
    'st_asi': 'delete',
    'st_pvi': '52230059512831',
    'st_sp': '2024-10-01%2013%3A08%3A07',
    'st_inirUrl': 'https%3A%2F%2Fwww.baidu.com%2Flink',
    'st_sn': '35',
    'st_psi': '20241002103747111-117001314791-0351363210',
}

headers = {
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'cache-control': 'no-cache',
    # 'cookie': 'websitepoptg_api_time=1727759287081; st_si=57823914314076; qgqp_b_id=5b9d12d1397dfb64500d7fa60eb92f4c; st_asi=delete; st_pvi=52230059512831; st_sp=2024-10-01%2013%3A08%3A07; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=35; st_psi=20241002103747111-117001314791-0351363210',
    'pragma': 'no-cache',
    'referer': 'https://guba.eastmoney.com/',
    'sec-ch-ua': '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'script',
    'sec-fetch-mode': 'no-cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0',
}



params = {
    'type': '0',
    'sort': '0',
    'page': '5', # 分页数
    'v': '2024_10_2_10_1',
}
# 请求获取列表接口入参数据
response = requests.get('https://gbcdn.dfcfw.com/rank/popularityList.js', params=params, headers=headers)
print(response.text) # var popularityList='WeUuVCsCeQIVUj......'
## 正则解析 var popularityList= 开头截取后边的所有数据；
popularityList = re.match(r"^var popularityList='(.*)'", response.text)
print(popularityList.group(1))



import execjs

# 编译JavaScript文件
with open('爬取东方财富_人气榜数据.js', 'r', encoding='utf-8') as f:
    js_content = f.read()
ctx = execjs.compile(js_content)

## 调用JavaScript中的getRandom函数
secids = ctx.call('func', popularityList.group(1))
print('处理后可用于列表请求的secids数据：', secids)

## 字符串格式化
# xx = 'kalsfd%sd' % (secids)
# print(xx)

# 发起列表数据请求
# # url = 'https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&np=3&ut=a79f54e3d4c8d44e494efb8f748db291&invt=2&secids=0.300077,0.300015,0.000002,0.300750,0.300925,0.002583,1.600460,1.601318,0.300180,0.300598,0.000750,1.600705,1.600584,0.300496,0.300663,0.300377,0.002594,0.300803,0.002049,1.601012&fields=f1,f2,f3,f4,f12,f13,f14,f152,f15,f16&cb=qa_wap_jsonpCB1727836696487';
# url = 'https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&np=3&ut=a79f54e3d4c8d44e494efb8f748db291&invt=2&fields=f1,f2,f3,f4,f12,f13,f14,f152,f15,f16&secids=0.300077,0.300015,0.000002,0.300750,0.300925,0.002583,1.600460,1.601318,0.300180,0.300598,0.000750,1.600705,1.600584,0.300496,0.300663,0.300377,0.002594,0.300803,0.002049,1.601012';
url = 'https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&np=3&ut=a79f54e3d4c8d44e494efb8f748db291&invt=2&fields=f1,f2,f3,f4,f12,f13,f14,f152,f15,f16&secids=%s' % (secids);
print(url)
response2 = requests.get(
    url = url,
    cookies=cookies,
    headers=headers,
)

res2 = response2.json()
print(res2)
total = res2['data']['total']
print('条数', total)
resList = res2['data']['diff']
# print(resList)

newList = []; # 接收处理后的返回数据
for r in resList:
    # print(r, type(r))

    # 修改键的值,并返回一个新的字典
    new_r = {}
    for key, val in r.items():
        if (key == 'f2'):
            new_key = '最新价'
            new_r[new_key] = r.get(key)
        elif (key == 'f3'):
            new_key = '涨跌幅'
            new_r[new_key] = r.get(key)
        elif (key == 'f4'):
            new_key = '涨跌额'
            new_r[new_key] = r.get(key)
        elif (key == 'f14'):
            new_key = '股票名称'
            new_r[new_key] = r.get(key)
        elif (key == 'f2'):
            new_key = '代码'
            new_r[new_key] = r.get(key)
        else:
            new_r[key] = r.get(key)
    # 将修改后的字典key 放入列表中
    newList.append(new_r);


print(newList)